package snsdbook.servlets;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import snsdbook.engine.Achievement;
import util.SQLUtil;

/**
 * Servlet implementation class ProfileServlet
 */
public class ProfileServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ProfileServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	  String username = (String)request.getParameter("username");
	  request.setAttribute("username", username);
		request.setAttribute("full_name", SQLUtil.Query(
		    "SELECT nama_lengkap FROM users WHERE username='" + username + "'")[0]);
    request.setAttribute("birthday", SQLUtil.Query(
        "SELECT birthday FROM users WHERE username='" + username + "'")[0]);
    request.setAttribute("email", SQLUtil.Query(
        "SELECT email FROM users WHERE username='" + username + "'")[0]);
    request.setAttribute("avatar", SQLUtil.Query(
        "SELECT avatar FROM users WHERE username='" + username + "'")[0]);
    request.setAttribute("about", SQLUtil.Query(
        "SELECT about FROM users WHERE username='" + username + "'")[0]);
    request.setAttribute("gender", SQLUtil.Query(
        "SELECT kelamin FROM users WHERE username='" + username + "'")[0]);
    request.setAttribute("profil", "true");
    
    String[] achievements_verbose = {
        "YOU NUB WAI POS - 1 Post",
        "WAAA CATZGODZ! - 10 Posts",
        "WAT SPIK YU AI KANNOT HIIR - 1 Comment",
        "NOISY I WANNA SLIPP - 20 Comments",
        "YOU AR VERI NAIZ PERSON! - 100 Likes on your post",
        "ALL UR DISLIKES ARE BILONG TU ME - 100 Dislikes on your post",
        "U BELIEBER! GO GO GO! - Create a post with title=bieber"
    };
    
    String[] t = null;
    int cnt = 0;
    
    t = SQLUtil.Query("SELECT id_berita FROM (SELECT id_berita, " +
                      "COUNT(id_likes) AS cnt FROM (SELECT berita.id_berita, " +
                      "id_likes FROM berita, likes WHERE berita.username = '" +
                      username +
                      "' AND berita.id_berita = likes.id_berita) AS T GROUP BY " +
                      "id_berita) AS K WHERE cnt >= 100");
    if (t.length > 0) {
      Achievement.PutAchievement(username, 4);
    }
    t = SQLUtil.Query("SELECT id_berita FROM (SELECT id_berita, " +
                      "COUNT(id_dislike) AS cnt FROM (SELECT berita.id_berita, " +
                      "id_dislike FROM berita, dislike WHERE berita.username = '" +
                      username +
                      "' AND berita.id_berita = dislike.id_berita) AS T GROUP BY " +
                      "id_berita) AS K WHERE cnt >= 100");
    if (t.length > 0) {
      Achievement.PutAchievement(username, 5);
    }
    t = SQLUtil.Query("SELECT id_berita FROM berita WHERE username='" + username + "' AND judul='bieber'");
    if (t.length > 0) {
      Achievement.PutAchievement(username, 6);
    }
    // Add achievements if not exists
    t = SQLUtil.Query("SELECT COUNT(id_komentar) FROM komentar WHERE komentator='" + username + "'");
    cnt = 0;
    if (t.length > 0) cnt = Integer.valueOf(t[0]);
    
    if (cnt >= 1) {
      Achievement.PutAchievement(username, 2);
    } else if (cnt >= 20) {
      Achievement.PutAchievement(username, 3);
    }
    request.setAttribute("comment_count", String.valueOf(cnt));
    // Add achievements if not exists
    t = SQLUtil.Query("SELECT COUNT(id_berita) FROM berita WHERE username='" + username + "'");
    cnt = 0;
    if (t.length > 0) cnt = Integer.valueOf(t[0]);
    
    if (cnt >= 1) {
      Achievement.PutAchievement(username, 0);
    } else if (cnt >= 10) {
      Achievement.PutAchievement(username, 1);
    }
    request.setAttribute("post_count", String.valueOf(cnt));
    
    // Achievements
    String[] achievements = SQLUtil.Query(
        "SELECT isi FROM achievement WHERE username='" + username + "'");
    
    ArrayList<String> archi = new ArrayList<String>();
    for (String a : achievements) {
      archi.add(achievements_verbose[Integer.valueOf(a)]);
    }
    
    request.setAttribute("achievements", archi);
    
    RequestDispatcher view = request.getRequestDispatcher("/Register.jsp");
    view.forward(request, response);
	}
}
